﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Security.Cryptography;

namespace ConsoleApp1
{
    public class DBHlper
    {
        const string sql = "Server=39.104.170.137,8082;Database=ComicsDB;user id=sa;password=Lsl123456;";
        static SqlConnection  con= null;


        public static void InitoCon() 
        {
            if (con == null) 
            {
                con = new SqlConnection(sql);
            }
            if (con.State == System.Data.ConnectionState.Closed) 
            {
                con.Open();
            }
            if (con.State == System.Data.ConnectionState.Broken) 
            {
                con.Close();
                con.Open();
            }
        }

        public static DataTable Select(string sql) 
        {
            InitoCon();
            SqlDataAdapter cm = new SqlDataAdapter(sql,con);
            DataTable dt = new DataTable();
            cm.Fill(dt);
            con.Close();
            return dt;
            
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static bool Insert(string sql) 
        {
            InitoCon();
            SqlCommand cm = new SqlCommand(sql, con);
            int num = cm.ExecuteNonQuery();
            con.Close();
            return num > 0;
        }

    }
}
